package com.emenu;

import java.util.ArrayList;
import java.util.List;

import com.emenu.FunhitFragment.Listview1Adapter;
import com.emenu.FunhitFragment.getMasTask;
import com.emenu.application.StaticObject;
import com.emenu.bean.PubPrinter;
import com.emenu.bean.PubPrinterTable;
import com.emenu.bean.PubTable;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class FunRetailPrint extends Fragment {
	public EditText rettable;
	public TextView rettablename;
	public EditText retprintedit;
	public Button chooseprint;
	public CheckBox retckb;
	public Button print;

	List<PubPrinter> PubPrinterlist = new ArrayList<PubPrinter>();
	//台号默认对应打印机
	List<PubPrinterTable> PubPrinterTablelist = new ArrayList<PubPrinterTable>();
	public StaticObject SO;
	Context cn;
	private String printcode = "";

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);

		SO = (StaticObject) activity.getApplication();
		PubPrinterlist = SO.getPubPrinterList();
		PubPrinterTablelist = SO.getPubPrinterTableList();
		cn = this.getActivity();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View v = inflater.inflate(R.layout.funretailprint, container, false);

		findViewsById(v);
		retprintedit.setText("默认打印机");

		setListener();

		return v;
	}

	/** 视图 样式 */
	private void findViewsById(View v) {
		rettable = (EditText) v.findViewById((R.id.rettable));
		rettablename = (TextView) v.findViewById((R.id.rettablename));
		retprintedit = (EditText) v.findViewById((R.id.retprintedit));
		chooseprint = (Button) v.findViewById((R.id.retchooseprint));
		print = (Button) v.findViewById((R.id.retprint));
		retckb = (CheckBox) v.findViewById((R.id.retckb));

	}

	/** 设置监听器 */
	private void setListener() {
		rettable.addTextChangedListener(txwatcher);
		chooseprint.setOnClickListener(chooseListener);
		print.setOnClickListener(printListener);

	}

	private TextWatcher txwatcher = new TextWatcher() {
		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
			// TODO Auto-generated method stub

		}

		@Override
		public void afterTextChanged(Editable s) {

			for (PubTable pt : SO.getPubTableList()) {
				if ((rettable.getText() + "").equals(pt.getHelpcode())) {
					rettablename.setText(pt.getName());

					// 默认打印机
					for (PubPrinterTable prt : PubPrinterTablelist) {
						if (pt.getCode().equals(prt.getTable_code())) {
							printcode = prt.getPrint_code();
							retprintedit.setText(prt.getPrintdesc());
						}

					}

				}

			}

		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {

		}

	};

	private OnClickListener chooseListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// //////
			int size1 = PubPrinterlist.size();

			String[] sArray = new String[size1];

			for (int i = 0; i < PubPrinterlist.size(); i++) {

				sArray[i] = (String) PubPrinterlist.get(i).getPrintdesc();
				
			}
			final String[] sArraytemp= sArray;
			AlertDialog.Builder builder = new AlertDialog.Builder(
					((Activity) cn).getParent());

			builder.setTitle("请选择打印机:");

			builder.setItems(sArray,

			new android.content.DialogInterface.OnClickListener() {

				public void onItemSelected(AdapterView<?> parent,

				View view, int position, long id) {

					// String str = Integer.toString(position);

//					printcode = PubPrinterlist.get(position).getCode();
//					retprintedit.setText(printcode);
					// showDialog("dd", str);

				}

				public void onNothingSelected(AdapterView<?> parent) {

					// TODO Auto-generated method stub
				}

				public void onClick(DialogInterface arg0, int arg1) {
					retprintedit.setText(sArraytemp[arg1]);
				};

			});
			// AlertDialog alert = builder.create();
			// alert.show();
			builder.show();

		}
	};

	private OnClickListener printListener = new OnClickListener() {

		@Override
		public void onClick(View v) {

		}

	};
}
